package com.atguigu.admin.servlet;

import com.atguigu.admin.filter.MyFilter;
import com.atguigu.admin.listener.MyServletContextListener;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.Arrays;

/**
 * @author 杨林
 * @create 2023-08-31 10:16 星期四
 * description:
 */

/**
 * 1、MyServlet --> /my
 * 2、DispatchServlet --> /
 */
@Configuration
public class MyRegistConfig {
    
    @Bean
    public ServletRegistrationBean myServlet(){
        MyServlet myServlet = new MyServlet();
        return new ServletRegistrationBean(myServlet,"/my","/my02");
    }
    
    @Bean
    public FilterRegistrationBean myFilter(){
        MyFilter myFilter = new MyFilter();
//        return new FilterRegistrationBean(myFilter,myServlet());
        FilterRegistrationBean<Filter> filterFilterRegistrationBean = new FilterRegistrationBean<>(myFilter);
        filterFilterRegistrationBean.setUrlPatterns(Arrays.asList("/my","/css/*"));
        return filterFilterRegistrationBean;
    }
    
    
    @Bean
    public ServletListenerRegistrationBean myListener(){

        MyServletContextListener myServletContextListener = new MyServletContextListener();
        return new ServletListenerRegistrationBean(myServletContextListener);

    }
}
